package com.google.code.greenplate;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class ShowMapFromTo extends MapActivity {
	
	private String mFromCity;
	private String mToCity;
	private Double fromLong;
	private Double fromLat;
	private Double toLong;
	private Double toLat;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        getIntentExtras();
        
        Geocoder fwdGeocoder = new Geocoder(this);       
        List<Address> fromLocations = null;
        List<Address> toLocations = null;
       
        try {
        	fromLocations = fwdGeocoder.getFromLocationName(mFromCity, 5);
        	toLocations = fwdGeocoder.getFromLocationName(mToCity, 5);       	

        	if ((fromLocations == null) && (toLocations == null)) {
        		Toast.makeText(this, "City information not found!" , Toast.LENGTH_SHORT).show();
        		return;
        	}
        	else {
        		if (fromLocations.size() > 0) {

        			Address fromLocation = fromLocations.get(0);
        			Log.d("GreenPlate", "after get(0)");
        			fromLong = fromLocation.getLongitude() * 1E6;
        			fromLat = fromLocation.getLatitude() * 1E6;
        		}
        		else {
        			Toast.makeText(this, "Departure location not found!", Toast.LENGTH_SHORT).show();
        			finish();
        		}

        		if (toLocations.size() > 0) {
        			Address toLocation = toLocations.get(0);
        			toLong = toLocation.getLongitude() * 1E6;
        			toLat = toLocation.getLatitude() * 1E6;       	
        		}
        		else {
        			Toast.makeText(this, "Destination not found!", Toast.LENGTH_SHORT).show();
        			finish();
        		}
        	}

        } catch (Exception e) {   
        	Log.d("GreenPlate:", "Exception:" + e);
        	Toast.makeText(this, "Exception: " + e.toString() , Toast.LENGTH_SHORT).show();
        }
      
        // Adds controls to map
        MapView mapView = (MapView) findViewById(R.id.map);  
        mapView.setBuiltInZoomControls(true);        
                  
        // Create a list of overlays with one marker
        MyItemizedOverlay itemizedoverlay1 = new MyItemizedOverlay
        	(getResources().getDrawable(R.drawable.marker1));
        
        // Create a list of overlays with another marker
        MyItemizedOverlay itemizedoverlay2 = new MyItemizedOverlay
        	(getResources().getDrawable(R.drawable.marker2));
        
        // Add an overlay using integers for lat,lng        
        GeoPoint point = new GeoPoint(fromLat.intValue(), fromLong.intValue());
        OverlayItem overlayitem1 = new OverlayItem(point, "From", mFromCity);   
        itemizedoverlay1.addOverlay(overlayitem1);
      
        point = new GeoPoint(toLat.intValue(), toLong.intValue());
        OverlayItem overlayitem2 = new OverlayItem(point, "To", mToCity);    
        itemizedoverlay2.addOverlay(overlayitem2);

        mapView.getOverlays().add(itemizedoverlay1); 
        mapView.getOverlays().add(itemizedoverlay2); 
        mapView.postInvalidate();
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private void getIntentExtras() {
		Bundle bundle = getIntent().getExtras();
		if (bundle == null) {
			Toast.makeText(this, "City information not found!", Toast.LENGTH_SHORT).show();
			return;
		}
		mFromCity = bundle.getString("fromCity") != null ? bundle.getString("fromCity") : "no fromCity";
		mToCity = bundle.getString("toCity") != null ? bundle.getString("toCity") : "no toCity";
	}
	
	private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
		
		private ArrayList<OverlayItem> myOverlays = new ArrayList<OverlayItem>();
		
		public MyItemizedOverlay(Drawable defaultMarker) {  
			super(boundCenterBottom(defaultMarker));
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			return myOverlays.get(i);
		}
		
		public void addOverlay(OverlayItem overlay) {    
			myOverlays.add(overlay);    
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);			
		}
		
		@Override
		protected boolean onTap(int i) {
			Toast.makeText(ShowMapFromTo.this, myOverlays.get(i).getSnippet(), Toast.LENGTH_LONG).show();
			return(true);
		} 
		
		@Override
		public int size() {
			return(myOverlays.size());
		}
	}
	
}
